package com.sysware.p2m.controller;

import com.alibaba.fastjson.JSONObject;
import com.sysware.p2m.entity.DataObject;
import com.sysware.p2m.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/data")
public class DataObjectController {
    @Autowired
    DataService dataService;

    @ResponseBody
    @RequestMapping(value = "getMyDataObjectList", method = RequestMethod.GET)
    public List<DataObject> getMyDataObjectList(@RequestParam String userId) {

        return dataService.getDataObjectList(userId, 1, 20);
    }

    @ResponseBody
    @RequestMapping(value = "saveDataObject", method = RequestMethod.GET)
    public JSONObject saveDataObject(/*@RequestBody DataObject dataObject*/) {
        List<DataObject> dataObjects = new ArrayList<>();
        for (int i = 200; i < 300; i++) {
            DataObject dataObject = new DataObject();
            dataObject.setId("d" + i);
            dataObject.setName("dataName-" + i);
            dataObject.setCreatorId("user1");
            dataObject.setCreateTime(new Date());
            dataObject.setStatusId("planing");

            dataObjects.add(dataObject);
        }

        dataService.saveDataObject(dataObjects);
        String[] ids = {"d1", "d2"};
        dataService.updateDataObject(ids, "approving");
        JSONObject result = new JSONObject();
        result.put("success", true);
        return result;
    }

}
